<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jul 14, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RClassesImporter::cls("html.AbstractHtmlCheckBox" );

class HtmlSqlLevelCheckBox extends AbstractHtmlCheckBox {
	
	var $_sql;
	var $_parentField;
	var $_orderBy;
	
	var $_ignoreId = null;
	var $_reviewMode = false;
	
	/**
	 * Constructor
	 *
	 * @param string $cbxName
	 * @param string $sql
	 * @param string - Name of parent Field $parentField
	 * @param array $valueOrArrayContain
	 */
	function __construct($cbxName, $sql, $parentField = 'parent_id', $valueOrArrayContain = null) {
		parent::__construct ( $cbxName, $valueOrArrayContain );
		$this->_sql = $sql;
		$this->_parentField = $parentField;
	}
	
	function setReviewMode($isReview = true) {
		$this->_reviewMode = $isReview;
	}
	
	/**
	 * Set datainfo
	 *
	 * @param string $sql
	 * @param string $parentField
	 * @param string $orderBy
	 */
	function setInfo($sql, $parentField, $orderBy = '') {
		$this->_sql = $sql;
		$this->_parentField = $parentField;
		$this->_orderBy = $orderBy;
	}
	
	/**
	 * Get tree
	 *
	 * @param string $emptyText
	 * @param number $parentid
	 * @param string $space
	 * @param array $trees
	 * @return array trees
	 */
	function _getTreeHtml($extAttributes, $htmlIdPrefix, $wrapper, $wraperAttributes, 
							$parentid = null, $level = 0) {

		$where = array();	
		if ($parentid === null) {
			$where[] = "{$this->_parentField} IS NULL";
		}
		$where[] = "{$this->_parentField} = ".intval ( $parentid );
		
		$where	= " WHERE " .implode(' OR ',$where);
		
		$sql = $this->_sql . $where;
		$res = SysDatabase::execute ( $sql );
		$level++;
		if ($level > 1) {
			$treeHtml = "<ul>";
		}
		while ( $rs = mysql_fetch_array( $res ) ) {
			if ($rs[0] == $this->_ignoreId) {
				continue;	
			}

			$checked = is_array($this->_selectedValues) && in_array($rs[0], $this->_selectedValues);
			$extAttr = "";
			if ($this->_reviewMode) {
				if ($checked) {
					$extAttr = 'disabled="disabled" ';
				}
				else {
					continue;
				}
			}
			
			$htmlId = "{$htmlIdPrefix}{$this->_chkName}{$rs[0]}";
			$extAttr .= "class=\"parent{$parentid} level{$level}\" onclick=\"updateCheckbox(this);\"";
			if ($level > 1) {
				$treeHtml .= "<li>";
			}
			if ($wrapper) {
				$treeHtml .= "<{$wrapper} {$wraperAttributes} class=\"level{$level}\">";
			}
			$treeHtml.= RHtmlHelper::createCheckBox($this->_chkName.'[]', $checked, $htmlId, $extAttr, $rs [0]);
			$treeHtml.= '<label for="'.$htmlId.'">'.$rs[1].'</label>';
			
			$treeHtml .= self::_getTreeHtml($extAttributes, $htmlIdPrefix, $wrapper, $wraperAttributes, $rs [0], $level );
			
			if ($wrapper) {
				$treeHtml .= "</{$wrapper}>";
			}
			if ($level > 1) {
				$treeHtml .= "</li>";
			}
		}
		if ($level > 1) {
			$treeHtml .= "</ul>";
		}
		return $treeHtml;
	}	
	/**
	 * Set ignore id
	 *
	 * @param int $ignoreId
	 */
	function setIgnoreId($ignoreId) {
		$this->_ignoreId = $ignoreId;
	}
	
	/**
	 * Create Combobox
	 *
	 * @param string $emptyText
	 * @param string $space
	 * @param string $cssCls
	 * @param string $style
	 * @param number $id
	 * @param string $extAttributes
	 * @return HTML to show combobox
	 */
	function createCheckBox($extAttributes = "", $htmlIdPrefix = "", $wrapper=null, $wraperAttributes = null) {
		SysResponse::addJavascriptContent('
			function updateCheckbox(ele) {
				var class= ".parent" + (ele.value);
				$(class).attr("checked", ele.checked);
				var display = ele.checked ? "block" : "none";
				$(ele.parentNode.getElementsByTagName("ul")[0]).css("display", display);
			}
			$(document).ready(function(){
				$(\'input:checked ~ ul\').css("display", "block");
			});
		');
		RCoreImporter::cls("helper.RHtmlHelper" );
		return $this->_getTreeHtml($extAttributes, $htmlIdPrefix, $wrapper, $wraperAttributes);
		if (!$this->_arrayData) {
			$this->_arrayData = self::_getTree ();
		}
		return $this->_arrayData;
		return parent::createCheckBox($extAttributes, $htmlIdPrefix, $wrapper, $wraperAttributes);
	}
}

?>